package cn.yzgy.yzgyoa.ucenter.service.impl;

import cn.yzgy.yzgyoa.ucenter.dto.SysStaffDTO;
import cn.yzgy.yzgyoa.ucenter.entity.SysStaff;
import cn.yzgy.yzgyoa.ucenter.entity.UserInfoDetail;
import cn.yzgy.yzgyoa.ucenter.mapper.SysStaffMapper;
import cn.yzgy.yzgyoa.ucenter.service.ISysStaffService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yqsen
 * @since 2021-06-03
 */
@Service
public class SysStaffServiceImpl extends ServiceImpl<SysStaffMapper, SysStaff> implements ISysStaffService {

    @Override
    public IPage<SysStaffDTO> getAllStaff(Page<SysStaffDTO> staffPage, SysStaffDTO queryDto) {

        return baseMapper.getAllStaff(staffPage,queryDto);
    }

    @Override
    public boolean addStaff(SysStaffDTO queryDto, UserInfoDetail currentUser) {
        SysStaff sysStaff = new SysStaff();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        queryDto.setCreateBy(currentUser.getUid()+"").setStaffState(true)
                .setCreateName(currentUser.getUserName())
                .setJoinTime(LocalDateTime.parse(queryDto.getJoinTimeString(),df));
        BeanUtils.copyProperties(queryDto,sysStaff);
        return sysStaff.insert();
    }

    @Override
    public boolean editStaff(SysStaffDTO queryDto, UserInfoDetail currentUser) {
        SysStaff sysStaff = new SysStaff();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        BeanUtils.copyProperties(queryDto,sysStaff);
        sysStaff.setUpdateBy(currentUser.getUid()+"")
                .setJoinTime(LocalDateTime.parse(queryDto.getJoinTimeString(),df));;
        return sysStaff.updateById();
    }

    @Override
    public boolean delStaff(SysStaffDTO queryDto, UserInfoDetail currentUser) {
        SysStaff sysStaff = new SysStaff();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        queryDto.setOutTime(LocalDateTime.parse(queryDto.getOutTimeString(),df));
        BeanUtils.copyProperties(queryDto,sysStaff);
        //设置为离职状态即可
        sysStaff.setStaffState(false).setUpdateBy(currentUser.getUid()+"").setOutTime(LocalDateTime.now());
        return sysStaff.updateById();
    }
}
